// ossApi.ts
import { api } from '@/api/api'; // 假设有封装好的 API 模块用于发送请求
import OSS from 'ali-oss';

// 阿里云 OSS 配置
const client = new OSS({
  region: 'oss-cn-beijing',
  accessKeyId: 'LTAI5tCCTsonpzgbA5od9dX1',
  accessKeySecret: 'Yxo3DXuIqm0DsZ4cIlNNlw4ssan4Jd',
  bucket: 'study-square',
});

// 生成签名 URL
export const generateSignedUrl = (objectKey: string) => {
  try {
    // 有效期设置为 3600 秒（即1小时）
    const url = client.signatureUrl(objectKey, { expires: 3600 });
    return url;
  }
  catch (error) {
    console.error('生成签名URL失败:', error);
    throw new Error('生成签名URL失败，请检查配置。');
  }
};

// 获取笔记内容的函数
export const getNoteContent = async (courseId: string, sourceId: string, resourceType: string) => {
  try {
    const response = await api.post('/java/ground/study/course/get_note', {
      sourceId,
      type: resourceType,
      noteContent: '',
      courseId,
    });
    return response.data;
  }
  catch (error) {
    console.error('获取笔记内容失败:', error);
    throw new Error('获取笔记内容失败');
  }
};

// 保存笔记内容的函数
export const saveNoteContent = async (courseId: string, sourceId: string, noteContent: string, resourceType: string) => {
  try {
    const response = await api.post('/java/ground/study/course/save_note', {
      sourceId,
      type: resourceType,
      noteContent,
      courseId,
    });
    return response.data;
  }
  catch (error) {
    console.error('保存笔记失败:', error);
    throw new Error('保存笔记失败');
  }
};

export { client };
